package com.googlecode.bizyap.core.tools;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.googlecode.bizyap.core.Globals;


public class CookieManager {

	public static void userLoggedOut(HttpServletRequest request, HttpServletResponse response){
		Cookie cookie = new Cookie(Globals.LOGGED_OUT_COOKIE, "true");
		cookie.setMaxAge(3600*24);
		cookie.setPath("/");
		response.addCookie(cookie);
	}

	public static void userLoggedIn(HttpServletRequest request, HttpServletResponse response){
		Cookie cookie = new Cookie(Globals.LOGGED_OUT_COOKIE, "true");
		cookie.setMaxAge(0);
		cookie.setPath("/");
		response.addCookie(cookie);
	}

	public static boolean didUserLogOut(HttpServletRequest request, HttpServletResponse response){
		Cookie[] cookies = request.getCookies();
		if(cookies!=null && cookies.length>0){
			for (Cookie cookie : cookies) {
				if(Globals.LOGGED_OUT_COOKIE.equals(cookie.getName())){
					return true;
				}
			}
		}
		return false;
	}
}
